using System;
using QFramework.Framework.Util;
using Object = UnityEngine.Object;

namespace QFramework.Framework.ResKit.Res
{
    public enum ResState
    {
        Waiting,
        Loading,
        Loaded
    }

    /// <summary>
    ///     资源对象：桥接模式
    /// </summary>
    public abstract class Res : SimpleRc
    {
        private ResState _state;
        protected string Path;

        public ResState State
        {
            get => _state;
            protected set
            {
                _state = value;

                if (_state == ResState.Loaded) OnLoadedEvent?.Invoke(this);
            }
        }

        public Object Asset { get; protected set; }

        public string Name { get; protected set; }

        public abstract bool LoadSync();

        public abstract void LoadAsync();

        protected abstract void OnReleaseRes();

        protected override void OnZeroRef()
        {
            OnReleaseRes();
        }

        private event Action<Res> OnLoadedEvent;

        public void RegisterOnLoadedEvent(Action<Res> onLoaded)
        {
            OnLoadedEvent += onLoaded;
        }

        public void UnRegisterOnLoadedEvent(Action<Res> onLoaded)
        {
            OnLoadedEvent -= onLoaded;
        }
    }
}